﻿<%@ Page Title="" Language="C#" MasterPageFile="~/TsSite.Master" AutoEventWireup="true" CodeBehind="ManageScheduledJobs.aspx.cs" Inherits="TireService.Web.View.AdminView.ManageScheduledJobs" %>

<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxCallback" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxPopupControl" TagPrefix="dx" %>
<%@ Register assembly="DevExpress.Web.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxPanel" tagprefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxRoundPanel" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxCallbackPanel" TagPrefix="dx" %>
<%@ Register assembly="DevExpress.Web.ASPxGridView.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxGridView" tagprefix="dx" %>
<%@ Register assembly="DevExpress.Web.ASPxEditors.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxEditors" tagprefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxLoadingPanel" TagPrefix="dx" %>



<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

<table cellpadding="0" cellspacing="2" border="0">
        <tr>
            <td>
                <img id="imgScheduledJobs" alt="" src="~/Images/Medium/cubes_yellow.png" runat="server" style="width: 32px; height: 32px" />
            </td>
            <td valign="middle">
                <h3><asp:Label ID="lblScheduledJobs" runat="server" Text="Planlagte server jobs"></asp:Label></h3>
            </td>
        </tr>
    </table>
    <hr />

    <!-- Javascript of action dropdown list -->
    <script language="javascript" type="text/javascript">

        // Function that handles actions selected in dropdown list
        function ActionClicked() {

            // Get selected action from dropdownlist
            var id = ddlAction.GetValue();

            if (id != null && id != 0) {
                if (id == 1) {
                    // TODO:
                }

                if (id == 2) {
                    // TODO:

                }

            }
            else {
                alert('Ingen handling valgt');
            }
        }

        function FilterScheduledJobs() {
            gvScheduledJobs.PerformCallback(ddlScheduledJobsFilter.GetValue());
        }

        function CreateJob() {
            // Show create job modal popup
            pcCreateJob.Show();
        }

        // Function that postback to server and runs job
        function RunJob(jobid) {
            var answer = confirm("Kør job nu?")
            if (answer) {
                LoadingPanel.Show();

                // Run callback function to run selected job
                callRunJob.PerformCallback(jobid);
            }
        }

        function OnCallbackRunJobCompleted(s, e) {
            LoadingPanel.Hide();

            // Refresh gridview with jobs
            gvScheduledJobs.PerformCallback(ddlScheduledJobsFilter.GetValue());

            alert('Job kørt');
        }

        // Function that postback to server and deletes job
        function DeleteJob(jobid) {
            var answer = confirm("Slet job?")
            if (answer) {
                LoadingPanel.Show();

                // Run callback function that deletes selected job
                callDeleteJob.PerformCallback(jobid);
            }
        }

        function OnCallbackDeleteJobCompleted(s, e) {
            LoadingPanel.Hide();

            // Refresh gridview with jobs
            gvScheduledJobs.PerformCallback(ddlScheduledJobsFilter.GetValue());

            //alert('Job blev slettet');
        }

        // Function that postback to server saves job to database
        function SaveNewJob() {
            
            // Validate values
            var jobtype = ddlJob.GetValue();

            if (jobtype == 0) {
                alert("Vælg job");
                return;
            }

            txtJobTitle.Validate();
            txtExecutionTime.Validate();
            
            var isCreateJobFormValid = ASPxClientEdit.ValidateGroup("CreateJob");
            if (isCreateJobFormValid) {

                var values = ddlJob.GetValue() + '|' + txtJobTitle.GetText() + '|' + txtExecutionTime.GetText() + '|' + cbDailyRepeat.GetChecked();

                callbackCreateJobPanel.PerformCallback(values);
            }
        }

        function OnCallbackCreateJobComplete(s, e) {
            // Reset form
            // TODO: Write code

            // Hide modal popup
            pcCreateJob.Hide();

            // Refresh gridview with jobs
            gvScheduledJobs.PerformCallback(ddlScheduledJobsFilter.GetValue());

            //alert('Job blev oprettet');
        }

        function CallbackError() {
            LoadingPanel.Hide();
            //alert('Fejl opstod - prøv venligst igen');
        }

    </script>                

    <dx:ASPxLoadingPanel ID="LoadingPanel" Text="Vent venligst..." ClientInstanceName="LoadingPanel" runat="server" Modal="True">
    </dx:ASPxLoadingPanel>

    <dx:ASPxCallback ID="callRunJob" ClientInstanceName="callRunJob" runat="server" OnCallback="callRunJob_Callback">
        <ClientSideEvents CallbackError="CallbackError" CallbackComplete="OnCallbackRunJobCompleted" />
    </dx:ASPxCallback>

    <dx:ASPxCallback ID="callDeleteJob" ClientInstanceName="callDeleteJob" runat="server" OnCallback="callDeleteJob_Callback">
        <ClientSideEvents CallbackError="CallbackError" CallbackComplete="OnCallbackDeleteJobCompleted" />
    </dx:ASPxCallback>
                
    <table border="0" cellspacing="0" cellpadding="5" width="100%">
        <tr>
            <td style="width:150px">
                <dx:ASPxComboBox ID="ddlAction" ClientInstanceName="ddlAction" runat="server" Width="150px">
                    <Items>
                        <dx:ListEditItem Selected="true" Value="0" Text="-- Handling --" />

                    </Items>
                </dx:ASPxComboBox>
            </td>
            <td style="width:75px">
                <dx:ASPxButton ID="btnSubmitAction" runat="server" Text="Udfør" AutoPostBack="False" UseSubmitBehavior="False" Width="75px">
                    <ClientSideEvents Click="function(s, e) { ActionClicked(); }" />
                </dx:ASPxButton>
            </td>
            <td style="width:75px">
                <dx:ASPxButton ID="btnCreateJob" runat="server" Text="Opret job" AutoPostBack="False" UseSubmitBehavior="False" Width="75px">
                    <ClientSideEvents Click="function(s, e) { CreateJob(); }" />
                </dx:ASPxButton>
            </td>
            <td>
                <dx:ASPxComboBox ID="ddlScheduledJobsFilter" ClientInstanceName="ddlScheduledJobsFilter" runat="server" Width="150px">
                    <Items>
                        <dx:ListEditItem Value="0" Text="Alle jobs" />
                        <dx:ListEditItem Selected="true" Value="1" Text="Ikke afviklede jobs" />
                    </Items>
                    <ClientSideEvents SelectedIndexChanged="function(s, e) { FilterScheduledJobs(); }" />
                </dx:ASPxComboBox>
            </td>
        </tr>
    </table>          
    <hr />
           
    <dx:ASPxGridView ID="gvScheduledJobs" runat="server" ClientInstanceName="gvScheduledJobs" 
        KeyFieldName="ScheduledJobQueueId" Width="100%" AutoGenerateColumns="False" EnableCallBacks="True" 
        OnCustomCallback="gvScheduledJobs_CustomCallback">
        <SettingsLoadingPanel Text="Henter&amp;hellip;" />
        <Settings ShowHeaderFilterButton="true" ShowGroupPanel="true" ShowFilterRow="True" />
        <SettingsPager AlwaysShowPager="True" PageSize="20" Position="TopAndBottom">
            <Summary 
                AllPagesText="Sider: {0} - {1} ({2} planlagte jobs)" 
                Text="Side {0} af {1} ({2} planlagte jobs)" />
        </SettingsPager>
        <SettingsText ConfirmDelete="Vil du slette planlagt job?" GroupPanel="Træk kolonne hertil for at gruppere efter den" HeaderFilterShowAll="Vis alle" />
        <SettingsBehavior ConfirmDelete="True" AllowGroup="true" AllowDragDrop="true" AllowFocusedRow="true" />
        <Columns>
            <dx:GridViewCommandColumn ShowSelectCheckbox="True" VisibleIndex="0">
                <HeaderTemplate>
                    <input type="checkbox" onclick="gvScheduledJobs.SelectAllRowsOnPage(this.checked);" style="vertical-align:middle;" title="Vælg/Fravælg alle linjer på siden"></input>
                </HeaderTemplate>
                <HeaderStyle Paddings-PaddingTop="1" Paddings-PaddingBottom="1" 
                    HorizontalAlign="Center">
                <Paddings PaddingTop="1px" PaddingBottom="1px"></Paddings>
                </HeaderStyle>
            </dx:GridViewCommandColumn>
            <dx:GridViewDataColumn FieldName="ScheduledJobQueueId" VisibleIndex="2" Caption="Id">
            </dx:GridViewDataColumn>
            <dx:GridViewDataColumn FieldName="Title" VisibleIndex="3" Caption="Title">
            </dx:GridViewDataColumn>
            <dx:GridViewDataColumn FieldName="ScheduledExecutionTime" VisibleIndex="4" Caption="Planlagt kørsel">
            </dx:GridViewDataColumn>
            <dx:GridViewDataColumn FieldName="RealisedExecutionTime" VisibleIndex="7" Caption="Afviklet">
            </dx:GridViewDataColumn>
            <dx:GridViewDataColumn FieldName="ScheduledJobId" Visible="false" VisibleIndex="8" Caption="Job">
            </dx:GridViewDataColumn>
            <dx:GridViewDataColumn VisibleIndex="9">
                <DataItemTemplate>
                    <dx:ASPxButton ID="btnRunJob" runat="server" ClientInstanceName="btnRunJob" Text="Kør job" ToolTip="Klik for at køre job" AutoPostBack="false"
                        ClientSideEvents-Click='<%# "function(s, e) { RunJob(" + Container.KeyValue + "); }" %>'>
                    </dx:ASPxButton>                                           
                </DataItemTemplate>
            </dx:GridViewDataColumn>
            <dx:GridViewDataColumn VisibleIndex="10">
                <DataItemTemplate>
                    <dx:ASPxButton ID="btnDeleteJob" runat="server" ClientInstanceName="btnDeleteJob" Text="Slet job" ToolTip="Klik for at slette job" AutoPostBack="false"
                        ClientSideEvents-Click='<%# "function(s, e) { DeleteJob(" + Container.KeyValue + "); }" %>'>
                    </dx:ASPxButton>                                           
                </DataItemTemplate>
            </dx:GridViewDataColumn>
        </Columns>
    </dx:ASPxGridView>

    <!-- Modalpopup: Create job -->
    <dx:ASPxPopupControl ID="pcCreateJob" Modal="true" runat="server" Height="1px" AutoUpdatePosition="true"
        PopupHorizontalAlign="WindowCenter" PopupVerticalAlign="WindowCenter"
        AllowDragging="True" ClientInstanceName="pcCreateJob" Left="100" Top="100"
        CloseAction="CloseButton" Width="175px" HeaderText="Opret job">
        <ContentCollection>
            <dx:PopupControlContentControl ID="PopupControlContentControl1" runat="server">
                <dx:ASPxCallbackPanel ID="callbackCreateJobPanel" ClientInstanceName="callbackCreateJobPanel" runat="server"
                    Width="320px" Height="100px" OnCallback="callbackCreateJobPanel_Callback" RenderMode="Table">
                    <ClientSideEvents EndCallback="OnCallbackCreateJobComplete" CallbackError="CallbackError" />
                    <PanelCollection>
                        <dx:PanelContent ID="PanelContent1" runat="server">
                            
                            <asp:Panel runat="server" ID="pCreateJob">
                                <table>
                                    <tr>
                                        <td>
                                            <dx:ASPxLabel ID="lblJobType" runat="server" Text="Job type">
                                            </dx:ASPxLabel>
                                        </td>
                                        <td>
                                            <dx:ASPxComboBox ID="ddlJob" ClientInstanceName="ddlJob" runat="server">
                                                <Items>
                                                    <dx:ListEditItem Value="0" Selected="true" Text="- Vælg job -" />
                                                    <dx:ListEditItem Value="1" Text="Webshop interface" />
                                                    <dx:ListEditItem Value="2" Text="Kunde service reminder" />
                                                    <dx:ListEditItem Value="3" Text="Benify ordre interface" />
                                                    <dx:ListEditItem Value="4" Text="Test job" />
                                                    <dx:ListEditItem Value="5" Text="Opret Benify kunder" />
                                                </Items>
                                            </dx:ASPxComboBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <dx:ASPxLabel ID="lblJobTitle" runat="server" Text="Titel">
                                            </dx:ASPxLabel>
                                        </td>
                                        <td>
                                            <dx:ASPxTextBox ID="txtJobTitle" ClientInstanceName="txtJobTitle" runat="server" Width="170px">
                                                <ValidationSettings CausesValidation="true" ValidationGroup="CreateJob" RequiredField-IsRequired="true" RequiredField-ErrorText="Indtast titel" SetFocusOnError="true" ErrorDisplayMode="ImageWithTooltip">
                                                    <RequiredField IsRequired="True" ErrorText="Indtast titel"></RequiredField>
                                                </ValidationSettings>
                                            </dx:ASPxTextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <dx:ASPxLabel ID="lblExecutionTime" runat="server" Text="Kørselsdato">
                                            </dx:ASPxLabel>
                                        </td>
                                        <td>
                                            <dx:ASPxDateEdit ID="txtExecutionTime" ClientInstanceName="txtExecutionTime" runat="server">
                                                <ValidationSettings CausesValidation="true" ValidationGroup="CreateJob" RequiredField-IsRequired="true" RequiredField-ErrorText="Angiv planlagt kørseldato" SetFocusOnError="true" ErrorDisplayMode="ImageWithTooltip">
                                                    <RequiredField IsRequired="true" ErrorText="Angiv planlagt kørseldato" />
                                                </ValidationSettings>
                                            </dx:ASPxDateEdit>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <dx:ASPxLabel ID="lblDailyRepeat" runat="server" Text="Kør hver dag">
                                            </dx:ASPxLabel>
                                        </td>
                                        <td>
                                            <dx:ASPxCheckBox ID="cbDailyRepeat" ClientInstanceName="cbDailyRepeat" runat="server">
                                            </dx:ASPxCheckBox>
                                        </td>
                                    </tr>                                
                                    <tr>
                                        <td colspan="2">
                                            <dx:ASPxButton ID="btnSaveNewJob" ClientInstanceName="btnSaveNewJob" runat="server" Text="Gem" AutoPostBack="False" UseSubmitBehavior="False">
                                                <ClientSideEvents Click="function(s, e) { SaveNewJob(); }" />
                                            </dx:ASPxButton>
                                        </td>
                                    </tr>
                                </table>
                                
                            </asp:Panel>

                        </dx:PanelContent>
                    </PanelCollection>
                </dx:ASPxCallbackPanel>
            </dx:PopupControlContentControl>
        </ContentCollection>
    </dx:ASPxPopupControl>

</asp:Content>
